package com.lagou.common.dao;

import com.lagou.common.pojo.Order;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface OrderDao extends JpaRepository<Order, Long> {
    List<Order> findAllByStatus(String status);

    /**
     * 过期后仍未支付的清单将被取消
     * @param orderId
     */
    @Modifying
    @Transactional
    @Query("update Order set status = 'overdue' where id = ?1 and status = 'paying'")
    void overdueAndCancel(Long orderId);

    List<Order> findAllByUserId(String userId);

    /**
     * 支付订单
     * @param orderId
     * @return
     */
    @Modifying
    @Transactional
    @Query("update Order set status = 'finish' where id = ?1 and status = 'paying'")
    int payOrder(Long orderId);
}
